// components/AirTemperature/AirTemperature.js
const API=require('../../config/api')
const db = wx.cloud.database()
var QQMapWX = require('../../datas/qqmap-wx-jssdk.js')
const MY_KEY = 'P2JBZ-LO6HJ-SVDFL-KHGTB-4ICP5-SAFXB'
var qqmapsdk = new QQMapWX({
    key: MY_KEY,
});;

Component({
  /**
   * 组件的属性列表
   */

  properties: {
 
  },

  /**
   * 组件的初始数据
   */
  data:{
    location: '108.312624,22.833925',
    oNcation:[],
    nowWather:[],   //实时天气
    dayWather:[],
    dateWather:[],
    indices:[],
    cityName:'南宁市',
    districtName:'西乡塘区'
  },
  options: {
    addGlobalClass: true,
  },

  
  attached() {
    // 在组件实例进入页面节点树时执行
      this.getAirTemperaTrue()
      
  },

  /**
   * 组件的方法列表
   */
  methods: {

    getAirTemperaTrue(){
    
         // 今天实时天气
      wx.request({
        url: API.nowWather,
        method:'GET',
         data:{location:this.data.location} ,
         success : (res)=>{
       this.setData({
        nowWather:res.data
       })
          // console.log('今天天气',res)
        }
      })
         //未来7天
    wx.request({
      url: API.dayWather,
      method:'GET',
       data:{location:this.data.location} ,
       success : (res)=>{
     this.setData({
      dayWather:res.data
     })
     this.triggerEvent('dayWathers',this.data.dayWather)
      }
    })

    //未来24小时
    wx.request({
      url: API.dateWather, 
      method:'GET',
       data:{location:this.data.location} ,
       success : (res)=>{
     this.setData({
      dateWather:res.data
     })
     //传值
     this.triggerEvent('dateWathers',this.data.dateWather)
     

      }
    })
     //舒适度
    wx.request({
      url: API.indices,
      method:'GET',
       data:{location:this.data.location,type:0} ,
       success : (res)=>{
     this.setData({
      indices:res.data
     })
       //传值
     this.triggerEvent('indexIndices',this.data.indices)
      }
    });

  
    },
    citySelect(){
      var that=this
    wx.navigateTo({
      url: '../../pages/city/city',
      events:{
        returnLocation:(res)=>{
          if(res.curCityLocation){
            this.setData({
              location:res.curCityLocation.location.lng+','+res.curCityLocation.location.lat,
              cityName:res.curCityLocation.city,
              districtName:res.curCityLocation.district
              
              })
           
           this.getAirTemperaTrue()
          }
      
        },
        //热门搜索
        rcurCity:(res)=>{
          if(res.curCity){
            this.setData({
              location:res.curCity.location.lng+','+res.curCity.location.lat,
              cityName:res.curCity.address_component.city,
              districtName:res.curCity.address_component.district
              })
           
           this.getAirTemperaTrue()
          

          }
        },
        //历史记录
        index:(res)=>{
      var index=res.index.currentTarget.dataset.index //定义索引
      db.collection('historical').get({                //获取数据库数据
        success: (res)=> {
          console.log(res.data);
          this.setData({     //根据点击显示
            location:res.data[index].location.lng+','+res.data[index].location.lat,
            cityName:res.data[index].city,
            districtName:res.data[index].district
            })
        }
      })

        }


        
      }
    })

    }
  }
})
